package com.chushouya.order.service.async;


import com.chushouya.manager.service.common.AlipayClientCommonService;
import com.chushouya.order.service.api.AlipayOpenMiniOrderCloseService;
import com.chushouya.order.service.api.AlipayOpenMiniOrderDeliveryModifyService;
import com.chushouya.order.service.api.AlipayOpenMiniOrderDeliveryReceiveService;
import com.chushouya.order.service.api.AlipayOpenMiniOrderDeliverySendService;
import com.general.framework.web.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.TimerTask;

/**
 * 订单模块异步工厂
 *
 * @author suhuishou team
 */
@Slf4j
public class OrderAlipayAsyncFactory {

    /**
     * 异步执行支付宝订单发货
     */
    public static TimerTask alipayOrderDelivery(Long orderId,Long finishAllDeliveryType) {
        return new TimerTask() {
            @Override
            public void run() {
                log.info("===>异步执行【支付宝订单发货】:{}", orderId);
                try {
                    SpringUtils.getBean(AlipayOpenMiniOrderDeliverySendService.class).orderDeliverySend(orderId,finishAllDeliveryType);
                } catch (Exception e) {
                    log.error("异步执行【支付宝订单发货】异常:{} {}", orderId, e.getMessage());
                }
            }
        };
    }

    /**
     * 异步执行支付宝订单收货
     */
    public static TimerTask alipayOrderReceive(Long orderId) {
        return new TimerTask() {
            @Override
            public void run() {
                log.info("===>异步执行【支付宝订单收货】:{}", orderId);
                try {
                    SpringUtils.getBean(AlipayOpenMiniOrderDeliveryReceiveService.class).orderDeliveryReceive(orderId);
                } catch (Exception e) {
                    log.error("异步执行【支付宝订单收货】异常:{} {}", orderId, e.getMessage());
                }
            }
        };
    }

    /**
     * 异步执行支付宝订单质检
     */
    public static TimerTask alipayOrderQuality(Long orderId) {
        return new TimerTask() {
            @Override
            public void run() {
                log.info("===>异步执行【支付宝订单质检】:{}", orderId);
                try {
                    SpringUtils.getBean(AlipayOpenMiniOrderDeliveryModifyService.class).confirmQuality(orderId);
                } catch (Exception e) {
                    log.error("异步执行【支付宝订单质检】异常:{} {}", orderId, e.getMessage());
                }
            }
        };
    }

    /**
     * 关闭订单
     */
    public static TimerTask alipayCloseOrder(Long orderId) {
        return new TimerTask() {
            @Override
            public void run() {
                log.info("===>异步执行【支付宝关闭订单】:{}", orderId);
                try {
                    SpringUtils.getBean(AlipayOpenMiniOrderCloseService.class).orderClose(orderId);
                } catch (Exception e) {
                    log.error("异步执行【支付宝关闭订单】异常:{} {}", orderId, e.getMessage());
                }
            }
        };
    }

    /**
     * 代用户确认订单
     */
    public static TimerTask alipayConfirmOrder(Long orderId, BigDecimal amount) {
        return new TimerTask() {
            @Override
            public void run() {
                log.info("===>异步执行【支付宝代用户确认订单】:{}", orderId);
                try {
                    SpringUtils.getBean(AlipayOpenMiniOrderDeliveryModifyService.class).confirmOrder(orderId);
                } catch (Exception e) {
                    log.error("异步执行【支付宝代用户确认订单】异常:{} {}", orderId, e.getMessage());
                }
            }
        };
    }
}
